import { Mapper, createMap, forMember, mapFrom } from "@automapper/core";
import { AutomapperProfile, InjectMapper } from "@automapper/nestjs";
import { Injectable } from "@nestjs/common";
import { Account } from "./dao";
import { ReadAccountDTO, CreateAccountDTO, UpdateAccountDTO, UpdateSelfDTO } from "./dto";

@Injectable()
export class AccountProfile extends AutomapperProfile {
    constructor(@InjectMapper() mapper: Mapper) {
        super(mapper);
    }

    override get profile() {
        return mapper => {
            createMap(
                mapper,
                Account,
                ReadAccountDTO,
                forMember(
                    dest => dest.roles,
                    mapFrom(source => source.roles)
                )
            );
            createMap(
                mapper,
                CreateAccountDTO,
                Account,
                forMember(
                    dest => dest.roles,
                    mapFrom(source => source.roles)
                )
            );
            createMap(mapper, UpdateSelfDTO, Account);
            createMap(
                mapper,
                UpdateAccountDTO,
                Account,
                forMember(
                    dest => dest.roles,
                    mapFrom(source => source.roles)
                )
            );
        };
    }
}
